package com.copa.l2287;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author copa
 * @createDate 2023-01-13 14:52
 * @function 哈希表计数法
 */
public class Test1 {

    public int rearrangeCharacters(String s, String target) {
        int count = Integer.MAX_VALUE;
        HashMap<Character, Integer> targetMap = new HashMap<>();
        char[] tempTarget = target.toCharArray();
        for (char c : tempTarget) {
            targetMap.put(c, targetMap.getOrDefault(c, 0) + 1);
        }
        HashMap<Character, Integer> sMap = new HashMap<>();
        char[] sTarget = s.toCharArray();
        for (char c : sTarget) {
            sMap.put(c, sMap.getOrDefault(c, 0) + 1);
        }
        Set<Map.Entry<Character, Integer>> entrySet = targetMap.entrySet();
        for (Map.Entry<Character, Integer> entry : entrySet) {
            count = Math.min(count, sMap.getOrDefault(entry.getKey(), 0) / entry.getValue());
            if (count == 0) {
                return count;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        String s = "ilovecodingonleetcode";
        String target = "code";
        System.out.println(new Test1().rearrangeCharacters(s, target));
    }
}
